GETOPT

Section: MINTLIB LIBRARY FUNCTIONS (3)
Updated: 3 March 1993
Index Return to Main Contents
 

NAME

getopt - get option letter from argument vector  

SYNOPSIS

#include <unistd.h>

int getopt(int argv, char * const *argv, const char *optstring);

extern char *optarg;

extern int optind;

extern int opterr;
 

DESCRIPTION

getopt returns the next option letter in argv that matches a letter in optstring. It supports all the rules of the UN*X System V command syntax standard (see the section RULES below). All new programs that wish to adhere to the command syntax standard should use getopt to parse positional parameters and check for options that are legal for that command. optstring must contain the option letters the command using getopt will recognize; if a letter is followed by a colon, the option is expected to have an argument, or group of arguments, which must be separated from it by white space. optarg is set to point to the start of the option-argument on return from getopt. getopt places in optind the argv index of the next argument to be processed. optind is external and is initialized to 1 before the first call to getopt. When all options have been processed (i.e., up to the first non-option argument), getopt returns EOF. The special option "--" may be used to delimit the end of the options; when it is encountered, EOF will be returned, and "--" will be skipped.  

DIAGNOSTICS

getopt prints an error message on standard error and returns a question mark (?) when it encounters an option letter not included in optstring or no option-argument after an option that expects one. This error message may be disabled by setting opterr to 0.  

RULES

The UN*X System V command standard contains the following rules:
 1. Command names must be between two and nine characters long.
 2. Command names must include only lower-case letters and digits.
 3. Option names must be one character long.
 4. All options must be preceded by "-".
 5. Options with no arguments may be grouped with a single "-".
 6. The first option-argument following an option must be
    preceded by white space.
 7. Option-arguments cannot be optional.
 8. Groups of option-arguments following an option must be either
    be separated by commas or separated by white space and quoted
    (e.g.,  -o xxx,z,yy  or  -o "xxx z yy").
 9. All options must precede operands on the command line. 10. "--" may be used to indicate the end of the options. 11. The order of the options relative to another should not matter. 12. The relative order of the operands may effect their

    significance in ways determined by the command with which
    they appear. 13. "-" preceded and followed by white space should only be used

    to mean standard input. getopt supports rules 3-10 above; the enforcement of the other rules must be done by the command itself.  

EXAMPLE

The following code fragment shows how one might process the
arguments for a command that can take the mutually exclusive
options 'a' and 'b', and the option 'o', which requires an
option-argument.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
extern char *optarg;

void main(int argc, char *argv[])
{
  int  c, aflg = 0, bflg = 0, errflg = 0;
  char *ofile = NULL;

  while ((c = getopt(argc, argv, "abo:")) != EOF)
    switch (c)
    {
      case 'a':
        if (bflg != 0)
          errflg++;
        else
          aflg++;
        break;
      case 'b':
        if (aflg != 0)
          errflg++;
        else
          bflg++;
        break;
      case 'o':
        ofile = optarg;
        break;
      case '?':
        errflg++;
    }
  if (errflg != 0)
  {
    fprintf(stderr, "Usage:...n");
    exit(1);
  }
  ...
}
 

NOTES

getopt cannot be used for complicated context-sensitive argument vector parsing. The UN*X System V standard may seem too restrictive; for instance, in the above example, '-ofile' is not allowed ('-o file' is). Some systems return -1 instead of EOF; this may actually make a difference on some systems.
 

Index

NAME
SYNOPSIS
DESCRIPTION
DIAGNOSTICS
RULES
EXAMPLE
NOTES

This document was created by man2html, using the manual pages.
Time: 11:14:52 GMT, June 22, 2025